package com.cloudcc.mobile.view.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.cloudframe.bus.MenuToggleEventR;
import com.cloudcc.cloudframe.util.PackageUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.bull.BullXunjianActivity;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.view.fragment.MenuRightFragment;
import com.cloudcc.mobile.weight.DialogUtils;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 关于cloudcc
 *
 * @author Administrator
 */
public class CloudccActivity extends BaseActivity implements IEventLife {
    private SlidingMenu menu_R;

    @Bind(R.id.tv1)
    TextView tvCloudcc;
    @Bind(R.id.tv2)
    TextView tvVersionName;
    @Bind(R.id.clickNet)
    TextView clickNet;
    @Bind(R.id.CallPhone)
    TextView CallPhone;
    @Bind(R.id.textBtn)
    TextView textBtn;
    @Bind(R.id.imageView1)
    ImageView imageview;
    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        initMenu();
        register();
        String fromAssets = getFromAssets();
        tvCloudcc.setText(Html.fromHtml(fromAssets));
        // 版本号
        String versionName = PackageUtils.getVersionName(mContext);
        tvVersionName.setText(StringUtils.isBlank(versionName) ? StringUtils.EMPTY
                : getString(R.string.Version_code) + versionName);

        imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                EventEngine.post(new MenuToggleEventR(false, false));

                Intent intent = new Intent(mContext,BullXunjianActivity.class);
                startActivity(intent);
//                menu_R.toggle();
            }
        });
    }

    private void initMenu() {
        Fragment leftMenuFragment = new MenuRightFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.id_Right_menu_frame, leftMenuFragment).commit();
        menu_R = new SlidingMenu(this);
        menu_R.setMode(SlidingMenu.RIGHT);
        menu_R.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE); // 触摸边界拖出菜单
        menu_R.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu_R.setOffsetFadeDegree(0.4f);
        menu_R.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        View viewRi = LayoutInflater.from(this).inflate(R.layout.right_menu_id,
                null);
        menu_R.setMenu(viewRi);
    }

    @Override
    public int getLayoutId() {
        return R.layout.cloudcc;
    }

    @OnClick(R.id.back)
    public void clickBack() {
        finish();
    }

    /*
     * 获取html文件
     */
    public String getFromAssets() {
        try {
            InputStreamReader inputReader = new InputStreamReader(
                    getResources().getAssets().open("company.txt"));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            while ((line = bufReader.readLine()) != null)
                Result += line;
            return Result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.clickNet, R.id.CallPhone})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.clickNet:
                Intent intent = new Intent();
                intent.setData(Uri.parse("http://weibo.com/shenzhouyundong"));//Url 就是你要打开的网址
                intent.setAction(Intent.ACTION_VIEW);
                this.startActivity(intent); //启动浏览器
                break;
            case R.id.CallPhone:
                showCallPhone(CloudccActivity.this);
                break;
        }
    }

    /**
     * 拨打电话
     *
     * @param context
     */
    public void showCallPhone(final Context context) {
        String mEns = RunTimeManager.getInstance().getlanguage();
        String zhuxiao = "";
        String zx = "";
        if ("en".equals(mEns)) {
            zhuxiao = "Telephone";
            zx = "Dear！ Make sure you dial the number ?";
        } else {
            zhuxiao = "电话";
            zx = "亲！确认拨打电话？";
        }
        DialogUtils.showWarn(mContext, zhuxiao,
                zx, new DialogUtils.DialogOnClickListener() {
                    @Override
                    public void onClickPositive(final Dialog dialog) {
                        Intent intent = new Intent(Intent.ACTION_CALL);
                        Uri data = Uri.parse("tel:" + "4006422008");
                        intent.setData(data);
                        startActivity(intent);
                        dialog.cancel();
                    }

                    @Override
                    public void onClickNegative(Dialog dialog) {
                        dialog.cancel();
                    }
                });
    }

    @OnClick(R.id.textBtn)
    public void onClick() {
        Intent intent = new Intent();
        intent.setData(Uri.parse("http://www.cloudcc.com"));//Url 就是你要打开的网址
        intent.setAction(Intent.ACTION_VIEW);
        this.startActivity(intent); //启动浏览器
    }

    @Override
    public void register() {
        EventEngine.register(this);
    }

    @Override
    public void unRegister() {
        EventEngine.uregister(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegister();
    }
    public void onEventMainThread(MenuToggleEventR event) {
        if (menu_R == null) {
            return;
        }
        if (event.isToggle) {

        } else {
            menu_R.toggle();
        }
    }
}
